home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Multimedia & Desktop / sk8 / SK8InJava / Code / Collections / Collection.java < prev    next >
Encoding:
Text File  |  1997-02-27  |  6.8 KB  |  287 lines  |  [TEXT/CWIE]

  1. /*  SK8 © 1997 Apple Computer, Inc.
  2.     This code is protected under the current SK8 License
  3.     See http://sk8.research.apple.com/ for more information
  4.     Apple Research Laboratories
  5. */
  6.  
  7.  
  8. //This file implements the collection protocol 
  9. //as well as some helper functions
  10.  
  11. public abstract class collection {    
  12.  
  13.     public abstract visitstate    initialvisitstate();   //returns null if empty
  14.                                     
  15.     public abstract boolean     isfinalvisitstate(visitstate inState);
  16.     
  17.     public abstract visitstate     succeedingvisitstate(visitstate inState);
  18.                                     
  19.     public abstract Object         elementatvisitstate(visitstate inState);
  20.                                     
  21.     public abstract void        setelementatvisitstate(visitstate inState, Object inElement) ;
  22.                                     
  23.     public abstract void        removevisitstate(visitstate inState);
  24.                                     
  25.     // in the lisp version of the List Protocol, 
  26.     // insertatvisitstate returns the collection.
  27.     // since the caller already has a handler to the collection
  28.     // to make the call, it makes more sense for function
  29.     // to return a more interesting value, like the visit
  30.     // state of the new element
  31.  
  32.     // If and only if the collection is empty, inState may be null
  33.     public visitstate insertatvisitstate(visitstate inState, Object inElement){
  34.         return insertatvisitstate(inState, inElement, false);
  35.     }
  36.                                     
  37.     public abstract visitstate    insertatvisitstate(visitstate inState, Object inElement, boolean inInsertAfter);
  38.                                     
  39.     public abstract int            indexatvisitstate(visitstate inState);
  40.                                     
  41.     public abstract visitstate    visitstateatindex(int inIndex);
  42.     
  43.     //must override this one!                                
  44.     protected void                 checkvisitstatetype(visitstate inState) {
  45.         throw new IllegalArgumentException("Incompatable visitstate class passed");
  46.     }
  47.  
  48.  
  49.     //public abstract Key            keyatvisitstate(int inColumn, visitstate inState);
  50.     //public abstract visitstate    visitstateatkey(Key inKey);
  51.     //public abstract void            setelementatkey(Key inKey, Object inElement);
  52.  
  53.     ///HELPERS!!!
  54.     public Object first() {
  55.         return this.elementatvisitstate(this.initialvisitstate());
  56.     }
  57.     
  58.     public Object last() {
  59.         return this.elementatvisitstate(this.finalvisitstate());
  60.     }
  61.     
  62.  
  63.     public visitstate finalvisitstate() {
  64.         visitstate vs;
  65.         
  66.         vs = this.initialvisitstate();
  67.         while ((vs != null) && (isfinalvisitstate(vs) != true))
  68.             vs = succeedingvisitstate(vs);
  69.  
  70.         return vs;
  71.     }
  72.     
  73.     public boolean empty () {
  74.         return (initialvisitstate() == null);
  75.     }
  76.  
  77.     public int length () {
  78.         visitstate vs = initialvisitstate();
  79.         int count = 0;
  80.         while (vs != null) {
  81.             count++;
  82.             if (isfinalvisitstate(vs) == true) {
  83.                 vs = null; }
  84.             else vs = succeedingvisitstate(vs);
  85.         }
  86.         return count;
  87.     }
  88.  
  89.  
  90.     public int position (Object obj) {
  91.         visitstate vs = initialvisitstate();
  92.         int count = 0;
  93.         if ((obj == null) || (vs == null))
  94.             return -1;
  95.         else {
  96.             int returnValue = -1;
  97.             while ((vs != null)) {
  98.                 count++;
  99.                 if (obj.equals(elementatvisitstate(vs))) {
  100.                     returnValue = count;
  101.                     vs = null; }
  102.                 else vs = succeedingvisitstate(vs);
  103.             }
  104.             return returnValue;
  105.         }
  106.     }
  107.  
  108.     public boolean contains (Object obj) {
  109.         return (! (this.position(obj) == -1));
  110.     }
  111.     
  112.  
  113.     //For stacks
  114.     public void push (Object obj1) {
  115.         visitstate vs = this.initialvisitstate();
  116.         this.insertatvisitstate(vs, obj1, false);
  117.     }
  118.     
  119.     
  120.     public void pushend (Object obj1) {
  121.         visitstate vs = this.finalvisitstate();
  122.         this.insertatvisitstate(vs, obj1, true);
  123.     }
  124.     
  125.     
  126.     
  127.     public Object pop () {
  128.         visitstate vs = initialvisitstate();
  129.         if (vs == null) {
  130.             return null;
  131.         } else {
  132.             Object obj = this.elementatvisitstate(vs);
  133.             this.removevisitstate(vs);
  134.             return obj;
  135.         }
  136.     }
  137.  
  138.     // 
  139.     public visitstate findvisitstateforelement (Object element) {
  140.         visitstate vs = initialvisitstate();
  141.         visitstate resultVS = null;
  142.         while ((resultVS == null) && (vs != null)) {
  143.             if (element == elementatvisitstate(vs))
  144.                 resultVS = vs;
  145.             if (isfinalvisitstate(vs) == true) {
  146.                 vs = null; }
  147.             else vs = succeedingvisitstate(vs);
  148.         }
  149.         return resultVS;
  150.     }
  151.  
  152.  
  153.     public void removeElement (Object element) {
  154.             visitstate vs = findvisitstateforelement(element); 
  155.             if (vs != null)
  156.                 removevisitstate(vs);
  157.     }
  158.  
  159.  
  160.     
  161.     //nth and setnth
  162.     
  163.     public Object nth (int n) {
  164.         visitstate vs = initialvisitstate();
  165.         int curcount = 1;
  166.         while ((curcount != n) && (vs != null)) {
  167.             curcount++;
  168.             if (isfinalvisitstate(vs) == true) {
  169.                 vs = null; }
  170.             else vs = succeedingvisitstate(vs);
  171.         }
  172.         if (vs == null)
  173.             return null;
  174.         else
  175.             return this.elementatvisitstate(vs);
  176.     }
  177.  
  178.     //Need the following function because in Java
  179.     //primitives like ints and floats are not "Objects"
  180.     public int nthint (int n) {
  181.         Integer res = (Integer)this.nth(n);
  182.         return res.intValue();
  183.     }
  184.     public float nthfloat (int n) {
  185.         Float res = (Float)this.nth(n);
  186.         return res.floatValue();
  187.     }
  188.  
  189.     
  190.     //Convient functions for nth element manipulations
  191.     public boolean setnth (int n, Object val) {
  192.         visitstate vs = initialvisitstate();
  193.         int count = 0;
  194.         while ((count != n) && (vs != null)) {
  195.  
  196.             count++;
  197.             if (isfinalvisitstate(vs) == true) {
  198.                 vs = null; }
  199.             else vs = succeedingvisitstate(vs);
  200.         }
  201.         if (vs == null)
  202.             return false;
  203.         else {
  204.             this.setelementatvisitstate(vs,val);
  205.             return true;
  206.         }
  207.     }
  208.     
  209.     public visitstate nthvisitstate(int n) {
  210.         if  ((n < 1 ) || (n > length()))
  211.             return null;
  212.         else {
  213.             int count = 1;
  214.             visitstate vs = initialvisitstate();
  215.             while ((count != n) && (vs != null)) {
  216.                 count++;
  217.                 vs = succeedingvisitstate(vs);
  218.             }
  219.             return vs;
  220.         }
  221.     }
  222.     
  223.     public void insertnth (int n, Object val) {
  224.         if (n == 1)
  225.             push(val);
  226.         else if (n == length() + 1)
  227.             pushend(val);
  228.         else if (( n < 1 ) || (n > length() + 1))
  229.             throw new IndexOutOfBoundsException("out of bounds!");
  230.         else {
  231.             visitstate vs = nthvisitstate(n);
  232.             insertatvisitstate(vs, val, false);
  233.         }
  234.     }
  235.  
  236.     public list range (int start, int end) {
  237.         list res = new list();
  238.         int currentcount = 0;
  239.         visitstate sscurrentstate = this.initialvisitstate();
  240.         Object it = null;
  241.         while (! (sscurrentstate == null)) {
  242.             it = this.elementatvisitstate(sscurrentstate);
  243.             currentcount = (currentcount + 1);
  244.             if ((currentcount >= start)) { 
  245.                 res.push(it);
  246.                 if ((currentcount >= end)) { 
  247.                     break;
  248.                     }
  249.             }
  250.             sscurrentstate = this.succeedingvisitstate(sscurrentstate);
  251.         }
  252.         return res.reverse();
  253.     }
  254.     
  255.     
  256.     // A printing function so collections print out nicely.
  257.     public void printcollection () {
  258.         Object currentActor;
  259.         visitstate vs = initialvisitstate();
  260.         while (vs != null) {
  261.             currentActor = elementatvisitstate(vs);
  262.             System.out.println(currentActor);
  263.             if (isfinalvisitstate(vs) == true) {
  264.                 vs = null; }
  265.             else vs = succeedingvisitstate(vs);
  266.         }
  267.     }
  268.     
  269.     public String toString(){
  270.         visitstate vs = this.initialvisitstate();
  271.         Object e;
  272.         String str = "list{";
  273.         
  274.         while (vs != null){
  275.             try{
  276.                 e = this.elementatvisitstate(vs);
  277.                 str = str + e.toString() + " ";
  278.                 vs = this.succeedingvisitstate(vs);
  279.             } catch (Exception exc) {
  280.                 str = str + "...}";
  281.                 break;
  282.             }
  283.         }
  284.         return str + "}";
  285.     }
  286.     
  287. }